import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Iterator;
import java.util.Scanner;
import java.util.StringTokenizer;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.service.BankService;
import com.acme.bankapp.service.Email;
import com.acme.bankapp.domain.Gender;
import com.acme.bankapp.service.*;

public class BankServer {
	static Bank bank = new Bank();
	public static void main(String[] args) throws IOException, InterruptedException {
		BankService service = new BankService();
		try {
			String s1 = "accounttype=s;balance=10;overdraft=0;firstname=A;lastname=A1;gender=m;";
			String s2 = "accounttype=s;balance=11;overdraft=-70;firstname=B;lastname=B1;gender=f;";
			String s3 = "accounttype=s;balance=10;overdraft=12;firstname=C;lastname=C1;gender=m;";
			String s4 = "accounttype=s;balance=10;overdraft=0;firstname=D;lastname=D1;gender=m;";
			PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter(args[0])));
			out.println(s1);
			out.println(s2);
			out.println(s3);
			out.println(s4);
			out.close();
		}
		catch (Exception e) {
			System.out.println("Error");
			return;
		}
		try {
			BufferedReader in = new BufferedReader(new FileReader(args[0]));
			String data = new String();
			do {
				data = in.readLine();
				if (data != null) {
					service.addClient(bank, data);
				}
			} while (data != null);
			in.close();
		}
		catch (IOException e) {
			System.out.println("Error in reading the file");
			return;
		}
		try {
			ServerSocket s = new ServerSocket(90);
			Socket fromClient = s.accept();
			Thread.sleep(5000);
			BufferedReader in = new BufferedReader(new InputStreamReader(fromClient.getInputStream()));
			ObjectOutputStream out = new ObjectOutputStream(fromClient.getOutputStream());
			String resultInput = in.readLine();
			StringTokenizer strResult = new StringTokenizer(resultInput,";");
			String helpString = strResult.nextToken();
			int index = resultInput.indexOf(';');
			String data = resultInput.substring(index + 1);
			if (helpString.equals("add")) {
				service.addClient(bank, data);
				service.printAllClients(bank);
				service.transferBank(bank, out);
				out.close();
			}
			else 
				if (helpString.equals("printall")) {
					service.printAllClients(bank);
					service.transferBank(bank, out);
					out.close();
				}
				else
					System.out.println("There is no such comand");
			in.close();
			fromClient.close();
			s.close();
		}
		catch (IOException e) {
			System.out.println("Socket is not open");
		}
		Iterator<Email> iter = bank.getServiceEmail().getQueue().getQueue().iterator();
		while(iter.hasNext() != false) {
			System.out.println(iter.next().getEmail());
		}
		bank.getServiceEmail().close();
		iter = bank.getServiceEmail().getQueue().getQueue().iterator();
		while(iter.hasNext() != false) {
			System.out.println(iter.next().getEmail());
		}
	}
}
